﻿using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Admin.Models.ConfigManager;
using Zocono.WCS.Admin.Models.User;
using Zocono.WCS.Application.AppConfigureManage;
using Zocono.WCS.Application.AppTraces;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;
using Zocono.WCS.Infrastructure.ApllicationDto.WCSTaskDto;

namespace Zocono.WCS.Admin.Controllers.ConfigManageMoudle
{
    public class JobInfoController : BaseController
    {
        private readonly IMapper _mapper;
        private readonly JobInfoApp _jobInfoApp;
        private readonly ProgrameInfoApp _programeInfoApp;

        public JobInfoController(IMapper mapper, JobInfoApp jobInfoApp, ProgrameInfoApp programeInfoApp, IMemoryCache memoryCache, TracesApp tracesApp, IHttpContextAccessor httpContextAccessor) : base(memoryCache, tracesApp, httpContextAccessor)
        {
            this._mapper = mapper;
            this._jobInfoApp = jobInfoApp;
            this._programeInfoApp = programeInfoApp;
        }

        public IActionResult Index()
        {
            return View();
        }


        ///// <summary>
        ///// 作业列表页面
        ///// </summary>
        ///// <returns></returns>
        //public async Task<ActionResult> JobInfoAsync()
        //{
        //    await AddTracesInfo();
        //    return View();
        //}
        /// <summary>
        /// 作业列表页面
        /// </summary>
        /// <returns></returns>
        public  ActionResult JobInfoAsync()
        {
            return View();
        }

        /// <summary>
        /// 获取所有作业列表
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> GetAllJobInfoAsync()
        {
            var where = new QueryJobInfoDto();
            where.PageIndex = (Start / Length) + 1;
            where.PageSize = Length;
            where.JobName = Request.Form["name"].ToString();
            where.ProgrameCode = Request.Form["code"].ToString();
            var jobList = await _jobInfoApp.GetPagedJobInfoAsync(where);
            var jobListDto = _mapper.Map<List<JobInfo>, List<JobInfoDto>>(jobList.Result);
            var pageData = new TableData<JobInfoDto>(Draw, jobList.Total, jobList.Total, jobListDto);
            //await AddTracesInfo(5, 0, "获取作业列表");
            return Json(pageData);
        }

        /// <summary>
        /// 添加或者编辑作业配置
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ActionResult> JobInfoAddOrEditAsync(long id)
        {
            var jobInfoPage = new JobInfoPageData();
            //获取程序列表 绑定数据程序列表
            var proList = await _programeInfoApp.GetProgrameInfoListAsync();
            jobInfoPage.ProgrameInfos = new List<ProgrameInfoPageData>();
            if (proList != null)
            {
                foreach (var pro in proList)
                {
                    jobInfoPage.ProgrameInfos.Add(new ProgrameInfoPageData() { ProgrameName = pro.ProgrameName, ProgrameCode = pro.ProgrameCode });
                }
            }
            if (id > 0)
            {
                var jobInfo = await _jobInfoApp.GetJobInfoByIdAsync(id);
                jobInfoPage.ID = jobInfo.ID;
                jobInfoPage.JobName = jobInfo.JobName;
                jobInfoPage.ProgrameCode = jobInfo.ProgrameCode;
            }
            return View(jobInfoPage);
        }

        /// <summary>
        /// 作业操作处理
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> OperateJobInfoAsync()
        {
            var jobInfo = new JobInfo();
            jobInfo.ID = Request.Form["id"].ToInt64();
            jobInfo.JobName = Request.Form["name"].ToString();
            jobInfo.ProgrameCode = Request.Form["proCode"].ToString();
            bool result;
            string msg;
            try
            {
                if (jobInfo.ID == 0)
                {
                    result = await _jobInfoApp.CreateJobInfoAsync(jobInfo);
                    await AddTracesInfo(2, 0, "新增作业配置");
                }
                else
                {
                    result = await _jobInfoApp.ModifyJobInfoAsync(jobInfo);
                    await AddTracesInfo(3, 0, "修改作业配置");
                }
                msg = result ? "成功" : "失败";
            }
            catch (Exception e)
            {
                result = false;
                msg = e.Message;
            }

            return CreateJsonGetResult(result, msg);
        }
        /// <summary>
        /// 删除作业配置
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> DelJobInfoAsync()
        {
            var id = Request.Form["id"].ToInt64();
            bool result;
            string msg;
            try
            {
                result = await _jobInfoApp.DelectJobInfoByIdAsync(id);
                msg = result ? "成功" : "失败";
            }
            catch (Exception e)
            {
                result = false;
                msg = e.Message;
            }
            await AddTracesInfo(4, 0, "删除作业配置");
            return CreateJsonGetResult(result, msg);
        }
    }
}
